<?
require_once 'BaseAction.php';

class AdminController extends TT_Base_Action
{    
    public function indexAction()
    {
        $this->view->params=$this->_getAllParams();
         $u=$this->getuser();
         $this->view->user=$u;
         if ($u->admin != 1 && $u->moderator != 1){                    //chuyen ve trang login neu khong phai la admin
            $this->_helper->getHelper('Redirector')->gotoUrlAndExit('/user/login');
        }                          
    }
    
    private function time1($str)            //  tuong tu ham time()
        {
            if($str=='')
                {
                    $time2=0;
                }
            else
                {
              $time1=explode("/",$str);
               
               $time2=mktime(0,0,0,$time1[1],$time1[0],$time1[2]);
                }
              return $time2;
        }
        
    private  function checkdate1($date)
        {
             $aDate_parts =explode("/",$date);


         if((int)$aDate_parts[0]!=0 && (int)$aDate_parts[2] !=0 && (int)$aDate_parts[2]!=0 )  
            {
                  if(checkdate($aDate_parts[1],$aDate_parts[0],$aDate_parts[2]))
                {
                    $a=1;
                }
            else
                {
                    $a=2 ;
                }  
            }
         else
            {
                $a=0;
            }
         return $a;
        }
   
    
    public function newAction()                // tao user moi
    {
        $this->view->params=$this->_getAllParams();
        $u=$this->getuser();
        $this->view->user=$u;
         if ($u->admin != 1 && $u->moderator != 1)
         {                    //chuyen ve trang login neu khong phai la admin
            $this->_helper->getHelper('Redirector')->gotoUrlAndExit('/user/login');
         }
        $u_model = $this->loadModel("Admins");         

        
        
        $chuc_danh=$u_model->chuc_danh();
        $this->view->chuc_danh=$chuc_danh;
        
        $phong_ban=$u_model->phong_ban();
        $this->view->phong_ban=$phong_ban;
        
        $trang_thai=$u_model->trang_thai();
        $this->view->trang_thai=$trang_thai;

        
          if ($this->getRequest()->isPost()) //neu nguoi dung submit form 
          { 
        
                        $username = trim($this->_getParam('username'));    
                        $password = $this->_getParam('password');
                        $password = md5($password);
                        $password2= $this->_getParam('password2');
                        $password2= md5($password2);
                        $name = $this->_getParam('name');
                        $birthday = $this->_getParam('birthday'); 

                        $hometown = $this->_getParam('hometown');
                        $address = $this->_getParam('address');
                        $phone = $this->_getParam('phone');
                       
                        
                                    $fileName = $_FILES['file']['name'];
                                    $tmpName  = $_FILES['file']['tmp_name'];
                                    $fileSize = $_FILES['file']['size'];
                                    $fileType = $_FILES['file']['type']; 

                                    $this->view->filetype=$fileType;
                         

                        
                        $phong_ban = $this->_getParam('phong_ban');
                        $chuc_danh = $this->_getParam('chuc_danh');
                        $tamtay_acc = $this->_getParam('tamtay_acc');
                        $yahoo_acc = $this->_getParam('yahoo_acc');
                        $tamtay_email = $this->_getParam('tamtay_email');
                        $date_join = $this->_getParam('date_join');

                        $date_out = $this->_getParam('date_out');
                        
                        $status = $this->_getParam('status');
                        $user_info = $this->_getParam('user_info');
               
               $checkbirthday=$this->checkdate1($birthday);
               if($date_join!="")
               {
                                $checkdate_join=$this->checkdate1($date_join);   
               }
               else
               {
                   $checkdate_join=1;
               }
                if($date_out!="")
                {
                                 $checkdate_out=$this->checkdate1($date_out);  
                }
                else
                {
                    $checkdate_out=1;
                }


        
       
              if($username !="" && $password !="d41d8cd98f00b204e9800998ecf8427e" && $name!="" && $birthday !="" && $phone!="" &&  $phong_ban!=""  && $chuc_danh!="" && $tamtay_email!="") //kiem tra cac dieu kien * bat buoc
                 {
           
                    if($password == $password2)
                        {
                           $check=$u_model->checkName($username); //kiem tra ten moi co nguoi dung chua?                                
                           if($check==true) // chua co ai dung`
                                {  
                                 if(strpos($username," "))
                                     {
                                                    $this->view->thongbao4=1;
                                     }  
                                  else
                                     { 
                                         if(strlen($username)<=100)
                                             {
                                                  if( $fileName=="" || $fileType=="image/gif" || $fileType=="image/jpeg" || $fileType=="image/bmp")
                                                         { 
                                                            if($checkbirthday==1 && $checkdate_join==1 && $checkdate_out==1 )
                                                                    {
                                                                             $birthday = $this->time1($birthday); 
                                                                 $date_out = $this->time1($date_out); 
                                                                 $date_join = $this->time1($date_join);
                                                                 
                                                                   if($birthday > -631177200 && $birthday < 946659600 )
                                                                           {
                                                                            if($date_join==0 ||($date_join > $birthday+473360400 ))   
                                                                              {
                                                                                   if($date_out==0 || $date_out>$date_join)
                                                                                      {
                                                                                        $pic="images/".$username.$fileName; 
                                                                                        $this->view->pic=$pic; 
                                                                                        move_uploaded_file($tmpName,$pic);
                                                                                        $pic="/".$pic;
                                                                                        $this->view->tb=0;
                                                                                         $this->view->thongbao0=1;   //thong bao da xong   
                                                                                         if($fileName=="")
                                                                                            {
                                                                                                $pic="";
                                                                                            }                                             
                                                                                         $a=$u_model->new_user($username,$password);
                                                                                         $b=$u_model->new_user_info($name, $birthday,$hometown,$address,$pic,$phong_ban,$chuc_danh,$tamtay_acc,$yahoo_acc,$tamtay_email,$date_join,$date_out,$status,$user_info,$phone);
                                                                                      }
                                                                                      else
                                                                                      {
                                                                                          $this->view->thongbao10=1;
                                                                                      }
                                                                              }
                                                                                 else
                                                                                    {
                                                                                        $this->view->thongbao9=1;
                                                                                    }
                                                                           }
                                                                   else
                                                                    {
                                                                        $this->view->thongbao8=1;
                                                                    }
                                                                   
                                                                    
                                                                    }
                                                                else
                                                                {
                                                                    $this->view->thongbao7=1;
                                                                }
                                                         
                                                         }
                                                            else
                                                            {
                                                            $this->view->thongbao6=1;
                                                            }      
                                                
                                             }   
                                           
                                         else
                                             {
                                                $this->view->thongbao5=1;
                                             }
   
                                     }
                                }
                          if($check==false) 
                                {
                                    $this->view->thongbao2=1;    // thong bao neu username da co nguoi dung
                                }
                                
                        }
                     else
                        {
                              $this->view->thongbao3=1; // thong bao neu pass xac nhan ko giòn
                        }
                 }
                 else
                 {
                        $this->view->thongbao1=1; // thong bao neu dua dien thong tin day du
                 }    
          }         
    } 
    
    public function addmoderatorAction()
        {
            $this->view->params=$this->_getAllParams();
            $u=$this->getuser();
            $this->view->user=$u;
            if ($u->admin != 1)
                 {                    //chuyen ve trang login neu khong phai la admin
                    $this->_helper->getHelper('Redirector')->gotoUrlAndExit('/user/login');
                 }
            
             $u_model = $this->loadModel("Admins");         
            
            if ($this->getRequest()->isPost()) //neu nguoi dung submit form 
                 { 
            
                            $username = trim($this->_getParam('username'));
                            $a=$u_model->addmoderator($username);
                          
                          $this->view->name=$username; 
     
                 }
                  $mod = $u_model->moderator(); 
                  $this->view->mod=$mod;
        }
        
        
     public function removemoderatorAction()
        { 
            $this->view->params=$this->_getAllParams();
            $u=$this->getuser();
            $this->view->user=$u;
            if ($u->admin != 1)
                 {                    //chuyen ve trang login neu khong phai la admin
                    $this->_helper->getHelper('Redirector')->gotoUrlAndExit('/user/login');
                 }
            
             $u_model = $this->loadModel("Admins");         
            
            if ($this->getRequest()->isPost()) //neu nguoi dung submit form 
                 { 
            
                            $username = trim($this->_getParam('username'));
                            $a=$u_model->removemoderator($username);
                          
                             $this->view->name=$username; 
      
                 }
                  $mod = $u_model->moderator(); 
                  $this->view->mod=$mod;
        }
    
    public function deleteuserAction()
        {
            $this->view->params=$this->_getAllParams();
            $u=$this->getuser();
            $this->view->user=$u;
            if ($u->admin != 1 && $u->moderator != 1)
                 {                    //chuyen ve trang login neu khong phai la admin
                    $this->_helper->getHelper('Redirector')->gotoUrlAndExit('/user/login');
                 }
                 
           $u_model = $this->loadModel("Admins");  
            
    
                $name = $this->_getParam('name'); 
                $this->view->name=$name;
                $uid=$u_model->checkinfo($name);
                
                $x=$this->_getParam('x');
                 if ($this->getRequest()->isPost() && $uid->name!=''&& $x=="") //neu nguoi dung submit form 
                    {
                       $link='/admin/deleteuser?x='.$uid->name;
                       $this->_helper->getHelper('Redirector')->gotoUrl($link);   
                                          
                    }
                
                if($x!="")
                {
                $username = $x;
                $uid_user=$u_model->checkinfo($username);
                $uid_user=$uid_user->uid;
                $this->view->test1=$uid_user;
                $this->view->uid=$uid;
                $info=$u_model->info($uid_user);
                $this->view->info=$info;
                $this->view->username=$username;
                if ($this->getRequest()->isPost())
                {
                $c=$u_model->deleteuser($username);
                $d=$u_model->deleteuser_info($uid_user);
                $link='/admin/deleteuser?del=1';
                $this->_helper->getHelper('Redirector')->gotoUrl($link);   
                }

                }

               
   
            
        }
    public function bannedAction()
        {
            $this->view->params=$this->_getAllParams();
            $u=$this->getuser();
            $this->view->user=$u;
            if ($u->admin != 1  && $u->moderator != 1)
                 {                    //chuyen ve trang login neu khong phai la admin
                    $this->_helper->getHelper('Redirector')->gotoUrlAndExit('/user/login');
                 }
            $u_model = $this->loadModel("Admins");  
            
             if ($this->getRequest()->isPost()) //neu nguoi dung submit form 
                {                      
                        $name = $this->_getParam('name');
                        $b=$u_model->banned($name);
                        $this->view->name=$name;
                }
                $a=$u_model->banned_user();
                $this->view->banned=$a;       
        }
    public function unbannedAction()
        {
            $this->view->params=$this->_getAllParams();
            $u=$this->getuser();
            $this->view->user=$u;
            if ($u->admin != 1 && $u->moderator != 1)
                 {                    //chuyen ve trang login neu khong phai la admin
                    $this->_helper->getHelper('Redirector')->gotoUrlAndExit('/user/login');
                 }
            $u_model = $this->loadModel("Admins");  
            
             if ($this->getRequest()->isPost()) //neu nguoi dung submit form 
                {                      
                        $name = $this->_getParam('name');
                        $b=$u_model->unbanned($name);
                        $this->view->name=$name;
                }
                $a=$u_model->banned_user();
                $this->view->banned=$a;       
        }
    public function editAction()
        {
            $this->view->params=$this->_getAllParams();
            $u=$this->getuser();
            $this->view->user=$u;
                if ($u->admin != 1 && $u->moderator != 1)
                     {                    //chuyen ve trang login neu khong phai la admin
                        $this->_helper->getHelper('Redirector')->gotoUrlAndExit('/user/login');
                     }
             $u_model = $this->loadModel("Admins"); 
             $params=$this->_getAllParams();
             $this->view->params=$params;
             $name_user=$params['user_name']; // lay bien $_get
            if($name_user!='')
                {
                    
                 
                 $check=$u_model->checkName($name_user); //kiem tra ten moi co nguoi dung chua?                                
                               if($check==true) // chua co ai dung`
                                {
                                  $this->_helper->getHelper('Redirector')->gotoUrl('/admin/edit?nouser=1');     
                                }
                                
                }
             
             $z=$u_model->checkInfo($name_user); // lay uid 
             
             $info=$u_model->info($z->uid);
             $this->view->info_user=$info;    
             
             $c=$u_model->chuc_danh(); //lay cac chuc danh 
             $this->view->chuc_danh=$c;
             
             $t=$u_model->trang_thai();
             $this->view->trang_thai=$t;
             
             $p=$u_model->phong_ban();
             $this->view->phong_ban=$p;  
                
              if ($this->getRequest()->isPost()) //neu nguoi dung submit form 
                     { 
                         $name         =$params['name']; 
                         $birthday     =$params['birthday'];
                         
                         $hometown     =$params['hometown'];
                         $address      =$params['address'];
                         $phone        =$params['phone'];
                         $pic          =$params['pic'];
                         $chuc_danh    =$params['chuc_danh'];
                         $phong_ban    =$params['phong_ban'];
                         $tamtay_acc   =$params['tamtay_acc'];
                         $yahoo_acc    =$params['yahoo_acc'];
                         $tamtay_email =$params['tamtay_email'];
                         $date_join    =$params['date_join'];
                        
                         $date_out     =$params['date_out'];
                        
                         $status       =$params['trang_thai'];
                         $user_info    =$params['user_info'];
                         $uid=$z->uid;
                       
                                    $fileName = $_FILES['file']['name'];
                                    $tmpName  = $_FILES['file']['tmp_name'];
                                    $fileSize = $_FILES['file']['size'];
                                    $fileType = $_FILES['file']['type'];
                                    
                                    $this->view->test1=$fileName; 
                                    
                       $flag=0;             
                       $this->view->thongbao1=$flag;
                        if($fileName !="")
                         {
                         if( $fileType=="image/gif" || $fileType=="image/jpeg" || $fileType=="image/bmp")
                                                         { 
                                                            $pic="images/".$name_user.$fileName; 
                                                            $this->view->pic=$pic; 
                                                            move_uploaded_file($tmpName,$pic);
                                                            $pic="/".$pic;
                                                         }
                        else
                            {
                                       $pic=$info->pic;
                                       $flag=1;    
                                       $this->view->thongbao1=$flag;
                            }
                        
                    }
                    else
                    {
                         $pic=$info->pic;  
                    }
                     
     $checkbirthday=$this->checkdate1($birthday);
     if($date_join!="")
               {
                                $checkdate_join=$this->checkdate1($date_join);   
               }
     else
               {
                   $checkdate_join=1;
               }
     if($date_out!="")
                {
                                 $checkdate_out=$this->checkdate1($date_out);  
                }
     else
                {
                    $checkdate_out=1;
                }
     if($checkbirthday==1 && $checkdate_join==1 && $checkdate_out==1 )
     {
        $birthday = $this->time1($birthday); 
        $date_out = $this->time1($date_out); 
        $date_join = $this->time1($date_join);  
                     
                 if($birthday > -631177200 && $birthday < 946659600 )
                  {
                      if($date_join==0 ||($date_join > $birthday+473360400 ))   
                      {
                          if($date_out==0 || $date_out>$date_join)
                          {        
                             if($flag==0)
                             {
                                                    $f=$u_model->edit($name,$birthday,$hometown,$address,$phone,$pic,$phong_ban,$chuc_danh,$tamtay_acc,$yahoo_acc,$tamtay_email,$date_join,$date_out,$status,$user_info,$uid);    
                             $this->view->flag=1;  
                             }
                          }
                          else
                          {
                          $this->view->thongbao10=1;
                          }
                      }
                      else
                      {
                      $this->view->thongbao9=1;                   
                      }
                  }
                  else
                  {
                  $this->view->thongbao8=1;
                  }
                  
     }
     else
     {
         $this->view->thongbao7=1;
     }
     


                     }
              $z=$u_model->checkInfo($name_user); // lay uid 
             
             $info=$u_model->info($z->uid);
             $this->view->info_user=$info;    
             
            
        }
        
    public function addAction()
        {
            $this->view->params=$this->_getAllParams();
            $u=$this->getuser();
            $this->view->user=$u;
                if ($u->admin != 1 && $u->moderator != 1)
                     {                    //chuyen ve trang login neu khong phai la admin
                        $this->_helper->getHelper('Redirector')->gotoUrlAndExit('/user/login');
                     }
            $u_model = $this->loadModel("Admins"); 
            $x=$this->_getParam('x');
            $this->view->x=$x;
            if($x=='phong_ban')
               {   
                   
                if ($this->getRequest()->isPost()) //neu nguoi dung submit form 
                     { 
                                $phong_ban = trim($this->_getParam('phong_ban'));
                                
                                $t=$u_model->phong_ban(); //lay danh sach phong ban
                                $n=count($t);
                                $flag=0;
                                for($i=0;$i<$n;$i++)
                                    {
                                        if($t[$i]->phong_ban == $phong_ban)
                                            {
                                                $flag=1;
                                            }
                                    }
                                
                                if($phong_ban!="" && $flag==0)
                                    {
                                        $add=$u_model->them_phong_ban($phong_ban);
                                        
                                    }
                                $this->view->flag=$flag; // cho ra thang bao
                     }
                     $tt=$u_model->phong_ban(); //lay danh sach phong ban moi
                     $this->view->phong_ban=$tt;
               }
               
               
            if($x=='trang_thai')
               {   
                   
                if ($this->getRequest()->isPost()) //neu nguoi dung submit form 
                     { 
                                $trang_thai = trim($this->_getParam('trang_thai'));
                                
                                $t=$u_model->trang_thai(); //lay danh sach phong ban
                                $n=count($t);
                                $flag=0;
                                for($i=0;$i<$n;$i++)
                                    {
                                        if($t[$i]->trang_thai == $trang_thai)
                                            {
                                                $flag=1;
                                            }
                                    }
                                
                                if($trang_thai!="" && $flag==0)
                                    {
                                        $add=$u_model->them_trang_thai($trang_thai);
                                        
                                    }
                                $this->view->flag=$flag; // cho ra thang bao
                     }
                     $tt=$u_model->trang_thai(); //lay danh sach phong ban moi
                     $this->view->trang_thai=$tt;
               }
               
                if($x=='chuc_danh')
               {   
                   
                if ($this->getRequest()->isPost()) //neu nguoi dung submit form 
                     { 
                                $chuc_danh = trim($this->_getParam('chuc_danh'));
                                
                                $t=$u_model->chuc_danh(); //lay danh sach phong ban
                                $n=count($t);
                                $flag=0;
                                for($i=0;$i<$n;$i++)
                                    {
                                        if($t[$i]->chuc_danh == $chuc_danh)
                                            {
                                                $flag=1;
                                            }
                                    }
                                
                                if($chuc_danh!="" && $flag==0)
                                    {
                                        $add=$u_model->them_chuc_danh($chuc_danh);
                                        
                                    }
                                $this->view->flag=$flag; // cho ra thang bao
                     }
                     $tt=$u_model->chuc_danh(); //lay danh sach phong ban moi
                     $this->view->chuc_danh=$tt;
               }
               
               
            
        }
      
    public function removeAction()
        {
            $this->view->params=$this->_getAllParams();
            $u=$this->getuser();
            $this->view->user=$u;
                if ($u->admin != 1 && $u->moderator != 1)
                     {                    //chuyen ve trang login neu khong phai la admin
                        $this->_helper->getHelper('Redirector')->gotoUrlAndExit('/user/login');
                     }
            $u_model = $this->loadModel("Admins"); 
            $x=$this->_getParam('x');
            $this->view->x=$x;
            if($x=='phong_ban')
               {   
                   
                if ($this->getRequest()->isPost()) //neu nguoi dung submit form 
                     { 
                                $phong_ban = trim($this->_getParam('phong_ban'));
                                
                                $t=$u_model->phong_ban(); //lay danh sach phong ban
                                $add=$u_model->xoa_phong_ban($phong_ban);

                     }
                     $tt=$u_model->phong_ban(); //lay danh sach phong ban moi
                     $this->view->phong_ban=$tt;
               }
            if($x=='trang_thai')
               {   
                   
                if ($this->getRequest()->isPost()) //neu nguoi dung submit form 
                     { 
                                $trang_thai = trim($this->_getParam('trang_thai'));
                                
                                $t=$u_model->trang_thai(); //lay danh sach phong ban
                                $add=$u_model->xoa_trang_thai($trang_thai);

                     }
                     $tt=$u_model->trang_thai(); //lay danh sach phong ban moi
                     $this->view->trang_thai=$tt;
               }    
               
            if($x=='chuc_danh')
               {   
                   
                if ($this->getRequest()->isPost()) //neu nguoi dung submit form 
                     { 
                                $chuc_danh = trim($this->_getParam('chuc_danh'));
                                
                                $t=$u_model->chuc_danh(); //lay danh sach phong ban
                                $add=$u_model->xoa_chuc_danh($chuc_danh);

                     }
                     $tt=$u_model->chuc_danh(); //lay danh sach phong ban moi
                     $this->view->chuc_danh=$tt;
               } 
               
        }
}
